(function(window, $, undefined){
		
	fnCallback.fnAddFunction('fnArea', function(oData){
		window.oLocationData = oData;
	});
	
	var sHtmlTpl = [
			"<input id='{{sHiddenId}}' type='hidden' name=\"{{name}}\" class='hidden'/>",
			"<div class=\"g-select\"><select class=\"g-left province\"><option value=\"none\">选择省</option></select></div>",
			"<div class=\"g-select\"><select class=\"g-left city\"><option value=\"none\">选择市</option></select></div>",
			"<div class=\"g-select\"><select class=\"g-left area\"><option value=\"none\">选择区</option></select></div>"
		].join(''), 
		sOptionTpl = "<option lid=\"{{id}}\" pid=\"{{pid}}\" name=\"{{name}}\" value=\"{{id}}\">{{name}}</option>";
		
	function fnResetSel(jqSel){
		var domOptions = jqSel[0].options,
			_innerHTML = jqSel.children()[0].innerHTML;
		domOptions.length = 0;
		domOptions.add(new Option(_innerHTML, 'none'));
		return jqSel;
	}
	
	function fnUpdate(jqNode, oObjectList){
		if(oObjectList){
			fnResetSel(jqNode);
			var aText = [];
			$.each(oObjectList, function(key, obj){
				aText.push($.ff.substitute(sOptionTpl, obj));
			}); 
			jqNode.append(aText.join(''));	
			jqNode.removeAttr('disabled');
		}
	}
	
	function fnInit(jqThis, oConfig){
		jqThis.html($.ff.substitute(sHtmlTpl, oConfig));
	}
	
	function fnSetSelectVal(jqSel, val, fnCall){
		if($.browser.msie && $.browser.version=="6.0") { 
			setTimeout(function(){ 
				jqSel.val(val); 
				fnCall && fnCall();
			},1); 
		}else { 
			jqSel.val(val); 
			fnCall && fnCall();
		}	
	}
	
	function fnGetSelectVal(jqSel, fnCall){
		if($.browser.msie && $.browser.version=="6.0") { 
			setTimeout(function(){ 
				jqSel.val(val); 
				fnCall && fnCall();
			},1); 
		}else { 
			jqSel.val(val); 
			fnCall && fnCall();
		}
	}
	
	function fnMain(jqThis, oConfig){
		var jqProvince = jqThis.find('.province'),
			jqCity = jqThis.find('.city'),
			jqArea = jqThis.find('.area'),
			jqHidden = jqThis.find('.hidden'),
			oCurrentProvince = null,
			oCurrentCity = null,
			iCurrentPositionLvs = 4;
			
		fnUpdate(jqProvince, oLocationData.all);
		
		//设置隐藏input的值
		function fnSetHiddenVal(){
			var jqValueNode = fnGetValueNode();
			if(iCurrentPositionLvs === 1){
				jqHidden.val(jqValueNode.val());
				jqHidden.attr('selectready', true);
			}else if(iCurrentPositionLvs === 3){
				jqHidden.val(jqValueNode.val())
				if(jqValueNode.is(jqArea)){
					jqHidden.attr('selectready', true);
				}else{
					jqHidden.attr('selectready', false);
				}
			}else if(iCurrentPositionLvs === 2){
				jqHidden.val(jqValueNode.val())
				if(jqValueNode.is(jqCity)){
					jqHidden.attr('selectready', true);
				}else{
					jqHidden.attr('selectready', false);
				}
			}else{
				jqHidden.val(jqValueNode.val())
			}
		}
		
		function fnGetValueNode(){
			var sAreaValue = jqArea.val(),
				sCityValue = jqCity.val(),
				sProvinceValue = jqProvince.val();
				
			if(sAreaValue!=='none'){
				return jqArea;
			}
			if(sCityValue!=='none'){
				return jqCity;
			}
			if(sProvinceValue!=='none'){
				return jqProvince;
			}
			return jqArea;
		}
		
		function fnProvinceOnChange(){
			var sId = $(this).val();
			jqCity.parent().show();
			if(sId === 'none'){
				fnResetSel(jqCity).attr('disabled', 'disabled');
				fnResetSel(jqArea).attr('disabled', 'disabled');
				return;
			}
			
			oCurrentProvince = oLocationData.all[sId];
			var _child = oCurrentProvince._child;
			
			//计算当前省级单位的所有层级
			if(_child === undefined){
				fnResetSel(jqCity).parent().hide();
				fnResetSel(jqArea).parent().hide();
				iCurrentPositionLvs = 1;
			}
			
			if(oCurrentProvince.pid === '-1' || oCurrentProvince.pid === '-2'){
				fnUpdate(jqCity, _child);
				fnResetSel(jqArea).parent().hide();
			}else{
				fnResetSel(jqArea).parent().show();
				jqArea.attr('disabled', 'disabled');
				fnUpdate(jqCity, _child);
			}
		}
		
		function fnCityOnChange(){
			var sId = $(this).val();
			if(sId === 'none'){
				fnResetSel(jqArea).attr('disabled', 'disabled');
				return;
			}
			oCurrentCity = oCurrentProvince._child[sId];
			if(oCurrentCity._child === undefined){
				jqArea.parent().hide();
				iCurrentPositionLvs = 2;
			}else{
				jqArea.parent().show();
				iCurrentPositionLvs = 3;
			}
			fnUpdate(jqArea, oCurrentCity._child);				
		}
		
		jqProvince.bind('change', fnProvinceOnChange);
		jqCity.bind('change', fnCityOnChange);
		jqThis.delegate('select', 'change', function(){
			fnSetHiddenVal();
		});
		
		if(oConfig.sDefaultIds){
			var sFids = oConfig.sDefaultIds.slice(1, oConfig.sDefaultIds.length-1),
				aLocation = sFids.split(',');
			jqHidden.val(aLocation[aLocation.length-1]);
			if(aLocation[0]){
				fnSetSelectVal(jqProvince, aLocation[0], function(){
					fnProvinceOnChange.call(jqProvince);
					if(aLocation[1]){
						fnSetSelectVal(jqCity, aLocation[1], function(){
							fnCityOnChange.call(jqCity);
							if(aLocation[2]){
								fnSetSelectVal(jqArea, aLocation[2]);
							}
						});
					}
				});
			}
		}
		
	}
	
	$.fn.pLocationSelect = function(oConfig){
		oConfig = $.extend(false, oDefaultConfig, oConfig);
		return this.each(function(){
			var jqThis = $(this);
			fnInit.call(jqThis, jqThis, oConfig);
			fnMain.call(jqThis, jqThis, oConfig);
		});
	}
	
	var oDefaultConfig = {}
	
})(window, jQuery);




